/*
 * PurchaseDetails.java
 * Created on 24-Apr-2011, 4:09:10 PM
 */
package pos.desktop;

import java.awt.Color;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import pos.desktop.controller.PurchaseController;
import pos.entity.Customer;
import pos.entity.Item;
import pos.entity.Order;
import pos.entity.PaymentDetails;
import pos.entity.PurchasedItem;
import pos.exception.POSException;
import pos.util.DisplayMessage;

/**
 * @since 30 MARCH 2011, 5:00 PM
 * @author Anup
 * <b>Description:<b> <br>This class is used to display a frame for
 * the customer to input the payment details, it is later authenticated
 * and the purchase authorized.
 */
public class PurchaseDetails extends javax.swing.JFrame {
    
    PaymentDetails paymentDetails = new PaymentDetails();
    PurchaseController purchaseController = new PurchaseController();
    ArrayList<Item> items = new ArrayList();
    Customer customer;
    Order order;
    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return Customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param customer 
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return Order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param order 
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public ArrayList<Item> getItems() {
        return items;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param items 
     */
    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public PaymentDetails getPaymentDetails() {
        return paymentDetails;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param paymentDetails 
     */
    public void setPaymentDetails(PaymentDetails paymentDetails) {
        this.paymentDetails = paymentDetails;
    }

    /** Creates new form PurchaseDetails */
    public PurchaseDetails() {
        initComponents();
        this.setLocation(400, 200);
        setSize(770, 450);
        setIconImage(Toolkit.getDefaultToolkit().getImage("..\\pos\\images\\JavaCup.gif"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField2 = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Purchase Details");
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 18));
        jLabel1.setText("Payment Details");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(290, 11, 165, 23);

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel2.setText("Please enter your payment details below.");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(269, 40, 237, 15);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 61, 763, 2);

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 11));
        jLabel3.setText("Shipping Details:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 69, 109, 15);

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel4.setText("Name*:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(16, 105, 45, 15);

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel5.setText("Address*:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(1, 141, 60, 15);

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel6.setText("ZIP*:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(21, 236, 40, 15);

        jTextField1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        getContentPane().add(jTextField1);
        jTextField1.setBounds(71, 102, 289, 21);

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(71, 141, 289, 81);

        jTextField2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        getContentPane().add(jTextField2);
        jTextField2.setBounds(71, 233, 289, 21);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(370, 69, 10, 198);

        jLabel7.setFont(new java.awt.Font("Verdana", 1, 11));
        jLabel7.setText("Cart Details:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(386, 69, 80, 15);

        jTextArea2.setColumns(20);
        jTextArea2.setEditable(false);
        jTextArea2.setFont(new java.awt.Font("Verdana", 0, 11));
        jTextArea2.setRows(5);
        jTextArea2.setWrapStyleWord(true);
        jScrollPane2.setViewportView(jTextArea2);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(386, 102, 367, 150);
        getContentPane().add(jSeparator3);
        jSeparator3.setBounds(0, 273, 763, 13);

        jLabel8.setFont(new java.awt.Font("Verdana", 1, 11));
        jLabel8.setText("Payment Details:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(10, 292, 110, 15);

        jLabel9.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel9.setText("Card Number:*");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(10, 321, 89, 15);

        jComboBox1.setFont(new java.awt.Font("Verdana", 0, 11));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "VISA", "MASTERCARD", "AMEX", "INTERAC" }));
        getContentPane().add(jComboBox1);
        jComboBox1.setBounds(105, 357, 260, 21);

        jLabel10.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel10.setText("Card Brand:*");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(19, 360, 78, 15);

        jTextField3.setFont(new java.awt.Font("Verdana", 0, 11));
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField3);
        jTextField3.setBounds(105, 318, 260, 21);

        jPasswordField1.setFont(new java.awt.Font("Verdana", 0, 11));
        getContentPane().add(jPasswordField1);
        jPasswordField1.setBounds(105, 396, 260, 21);

        jLabel12.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel12.setText("Security #:*");
        getContentPane().add(jLabel12);
        jLabel12.setBounds(30, 400, 72, 15);

        jLabel11.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel11.setText("Card Type:*");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(430, 321, 72, 15);

        jComboBox2.setFont(new java.awt.Font("Verdana", 0, 11));
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Credit", "Debit", "Prepaid" }));
        getContentPane().add(jComboBox2);
        jComboBox2.setBounds(520, 318, 233, 21);

        jLabel13.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel13.setText("Expiry(MM/YYYY):*");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(393, 360, 109, 15);

        jTextField5.setFont(new java.awt.Font("Verdana", 0, 11));
        getContentPane().add(jTextField5);
        jTextField5.setBounds(520, 357, 104, 21);

        jLabel14.setFont(new java.awt.Font("Verdana", 0, 11));
        jLabel14.setText("/");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(628, 359, 9, 17);

        jTextField6.setFont(new java.awt.Font("Verdana", 0, 11));
        getContentPane().add(jTextField6);
        jTextField6.setBounds(641, 357, 112, 21);

        jButton2.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton2.setText("Purchase");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(626, 396, 127, 23);

        jButton3.setFont(new java.awt.Font("Verdana", 0, 11));
        jButton3.setText("Cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(493, 396, 127, 23);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param evt 
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jLabel2.setForeground(Color.BLACK);
        jLabel2.setText("Please enter your payment details below.");
        if (checkForMandtoryFields()) {
            jLabel2.setText("Please fill all the mandatory details");
            jLabel2.setForeground(Color.RED);
            return;
        }
        try {
            addPaymentDetail();
            purchaseController.processPurchase(getOrder(), getPaymentDetails());
            JOptionPane.showMessageDialog(this, "You order has been successfully recorded, and would be"
                +" depatched for delivery soon", "Order Confirmed. Order Id:"+getOrder().getOrderId(), JOptionPane.PLAIN_MESSAGE);
            setOrder(null);
            getItems().clear();
            this.dispose();
        } catch (POSException ex) {
            jLabel2.setText(ex.getMessage());
            jLabel2.setForeground(Color.RED);
            setOrder(null);
            getItems().clear();   
        } catch (SQLException ex) {
            jLabel2.setText(ex.getMessage());
            jLabel2.setForeground(Color.RED);
            setOrder(null);
            getItems().clear();
        }    
}//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param evt 
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //close the window
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param evt 
     */
    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     */
     private void addPaymentDetail(){
        PaymentDetails tempPaymentDetails = getPaymentDetails();
        tempPaymentDetails.setPaymentCardBrand(jComboBox1.getSelectedItem().toString());
        tempPaymentDetails.setCardType(jComboBox2.getSelectedItem().toString());
        tempPaymentDetails.setPaymentCardNumber(jTextField3.getText().trim());
        tempPaymentDetails.setExpiryMonth(jTextField5.getText().trim());
        tempPaymentDetails.setExpiryYear(jTextField6.getText().trim());
        tempPaymentDetails.setSecurityNumber(new String(jPasswordField1.getPassword()));
        tempPaymentDetails.setPhoneNumber(customer.getPhoneNumber());
        tempPaymentDetails.setCardNumber("");
        setPaymentDetails(tempPaymentDetails);
    }
    
     /**
      * @since 30 MARCH 2011, 5:00 PM
      */
    void displayItems(){
        //Adding the customer details
        Customer customer = getCustomer();
        jTextField1.setText(customer.getName()+" "+customer.getLastName());
        StringBuilder address = new StringBuilder("");
        address.append(customer.getUnitNumber()).append(" ").append(customer.getAddress()).append("\n");
        address.append(customer.getCity()).append("\n");
        address.append(customer.getCountry()).append("\n");
        jTextArea1.setText(address.toString());
        jTextField2.setText(customer.getPostalCode());
        
        //Adding the Purchase details
        StringBuilder builder = new StringBuilder();
        Double total = 0.0;
        setOrder(purchaseController.createOrder(getItems()));
        ArrayList<PurchasedItem> tempPurchasedItems = getOrder().getPurchasedItems();
        ArrayList<Item> tempItems = getItems();
        for(int i = 0; i < tempPurchasedItems.size(); i++){
            PurchasedItem purchasedItem = tempPurchasedItems.get(i);
            Item item = tempItems.get(i);
            purchaseController.calculatePriceAdjustments(purchasedItem, item);
            builder.append(purchasedItem.getName()).append(" (").append(String.valueOf(item.getUPCode())).append(")");
            builder.append("\t $ ").append(purchasedItem.getPriceAdjustedAmount()).append("\n");
            total = total+purchasedItem.getPriceAdjustedAmount();
        }
        getOrder().setOrderTotalPrice(total + getOrder().getOrderTotalTaxes());
        builder.append("\n\nNet Price: $ ").append(DisplayMessage.roundTwoDecimals(total));
        builder.append("\nTax: $ ").append(DisplayMessage.roundTwoDecimals(getOrder().getOrderTotalTaxes()));
        builder.append("\nGrand Total: $ ").append(DisplayMessage.roundTwoDecimals(getOrder().getOrderTotalPrice()));
        jTextArea2.setText(builder.toString().trim());
    }
    
    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return boolean
     */
    private boolean checkForMandtoryFields() {
        boolean flag = true;
        if((jTextField3.getText() == null)||(jTextField3.getText().equalsIgnoreCase(""))){
            return flag;
        }else if((jTextField6.getText() == null)||(jTextField6.getText().equalsIgnoreCase(""))){
            return flag;
        }else if((jTextField5.getText() == null)||(jTextField5.getText().equalsIgnoreCase(""))){
            return flag;
        }else if((jPasswordField1.getPassword() == null) || (jPasswordField1.getPassword().toString().equalsIgnoreCase(""))){
            return flag;
        }else if((jTextField1.getText() == null)||(jTextField1.getText().equalsIgnoreCase(""))){
            return flag;
        }else if((jTextField2.getText() == null)||(jTextField2.getText().equalsIgnoreCase(""))){
            return flag;
        }else if((jTextArea1.getText() == null)||(jTextArea1.getText().equalsIgnoreCase(""))){
            return flag;
        }
        flag = false;
        return flag;
    }
}
